using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Silvery
{
    /// <summary>
    /// 标准基本游戏物体基类
    /// </summary>
    public interface IBaseObject : IDisposable
    {
        TransformGroup TransformGroup { get; }
        ITimer ObjectTimer { get; }
        string ClassName { get;  }
        event PositionEventHandler PositionChanged;
        Point Position { get; set; }
        double X { get; set; }
        double Y { get; set; }
        int Z { get; set; }
        bool IsVisible { get; set; }
    }
    public delegate void PositionEventHandler(object sender, DependencyPropertyChangedEventArgs e);
    /// <summary>
    /// 基本物体类
    /// </summary>
    public class BaseObject : Canvas, IBaseObject
    {

        /// <summary>
        /// 物体时间标记接口
        /// </summary>
        public ITimer ObjectTimer { get; protected set; }
        /// <summary>
        /// 位置发生变化时候调用的类
        /// </summary>
        public event PositionEventHandler PositionChanged;
        /// <summary>
        /// 类名
        /// </summary>
        public string ClassName
        {
            get
            {
                Type T = this.GetType();
                return T.Name;
            }
        }
        /// <summary>
        /// 构造
        /// </summary>
        public BaseObject()
        {            
            var tg =  new TransformGroup();
            this.RenderTransform = tg;
            tg.Children.Add(new TranslateTransform());
            tg.Children.Add(new ScaleTransform());
            tg.Children.Add(new RotateTransform());
            ObjectTimer = new CallTimer();
            this.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            this.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        }
        /// <summary>
        /// 返回变换集合
        /// 在WP7是无效,后续开发版本
        /// </summary>
        public TransformGroup TransformGroup
        {
            get { return RenderTransform as TransformGroup; }
        }
        /// <summary>
        /// 改变位置到一个指定的点
        /// </summary>
        /// <param name="position"></param>
        protected virtual void ChangePositionTo(Point position)
        {
            (TransformGroup.Children[0] as TranslateTransform).X = position.X;
            (TransformGroup.Children[0] as TranslateTransform).Y = position.Y;
            
        }
        /// <summary>
        /// 位置信息
        /// </summary>
        public Point Position
        {
            get { return (Point)GetValue(PositionProperty); }
            set { SetValue(PositionProperty, value); }
        }
        public double X { get { return Position.X; } set { Position = new Point(value,Y); } }
        public double Y { get { return Position.Y; } set { Position = new Point(X, value); } }
        public static readonly DependencyProperty PositionProperty = DependencyProperty.Register(
            "Position",
            typeof(Point),
            typeof(BaseObject),
            new PropertyMetadata(ChangePositionProperty)
        );
        public static void ChangePositionProperty(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BaseObject objectBase = d as BaseObject;
            var pt = (Point)e.NewValue;
            objectBase.ChangePositionTo(pt);

            //(objectBase.TransformGroup.Children[1] as ScaleTransform).CenterX = pt.X;
            //(objectBase.TransformGroup.Children[1] as ScaleTransform).CenterY = pt.Y;
            //(objectBase.TransformGroup.Children[2] as RotateTransform).CenterX = pt.X;
            //(objectBase.TransformGroup.Children[2] as RotateTransform).CenterY = pt.Y;
            if (objectBase.PositionChanged != null) { objectBase.PositionChanged(objectBase, e); }
        }
        /// <summary>
        /// 获取或设置Z层次深度
        /// </summary>
        public int Z
        {
            get { return Canvas.GetZIndex(this); }
            set { Canvas.SetZIndex(this, value); }
        }

        #region IDisposable Members
        /// <summary>
        /// 销毁方法，可重构
        /// </summary>
        public virtual void Dispose()
        {
            RenderTransform = null;
            if (this.Parent is IBaseObjCollection)
                (Parent as IBaseObjCollection).RemoveBaseObj(this);
            if (this.Children.Count > 0)
            {
                this.Children.Clear();
            }
        }

        #endregion
        /// <summary>
        /// 是否显示
        /// </summary>
        public bool IsVisible
        {
            get
            {
                if (this.Visibility == System.Windows.Visibility.Collapsed)
                    return false;
                else
                    return true;
            }
            set
            {
                if (value)
                {
                    Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }
        private bool isMirror;

        public bool IsMirror
        {
            get { return isMirror; }
            set 
            {
                if (isMirror == value)
                    return;
                isMirror = value;
                if (value)
                    (this.TransformGroup.Children[1] as ScaleTransform).ScaleX = -1;
                else
                    (this.TransformGroup.Children[1] as ScaleTransform).ScaleX = 1;
            }
        }
        public double RotateAngle
        {
            get { return (this.TransformGroup.Children[2] as RotateTransform).Angle; }
            set { (this.TransformGroup.Children[2] as RotateTransform).Angle = value ; }
        }
    }
}
